# Maintainer: Antonio Rojas <arojas@archlinux.org>

pkgname=genxrdpattern
pkgver=1.1
pkgrel=3
pkgdesc='Uses ObjCryst++ to generate a theoretical XRD pattern'
arch=(loong64 x86_64)
url='https://github.com/psavery/genXrdPattern'
license=(GPL)
depends=(gcc-libs)
makedepends=(cmake)
source=(https://github.com/psavery/genXrdPattern/archive/$pkgver-linux/$pkgname-$pkgver.tar.gz
        https://github.com/psavery/genXrdPattern/releases/download/$pkgver-linux/objcryst.tgz)
sha256sums=('9bc01dbf637d6520d8e2f5743bb1b46f8319432af5ce2d2a484a36615c1586d3'
            '5523eae9937388260b3e491a1a11a443ab00a0961318ec92eee08f9366a0953f')

prepare() {
  sed -e 's|/user/psavery/src/objcryst/Fox/src|.|g' -i objcryst/Fox/src/Fox.dep
  sed -e 's|-march=native||' -i objcryst/ObjCryst/*.mak # Don't use -march=native
  sed -e 's|set(CMAKE_EXE_LINKER_FLAGS \"-static\")|set(CMAKE_EXE_LINKER_FLAGS "$ENV{LDFLAGS}")|' \
    -i genXrdPattern-$pkgver-linux/CMakeLists.txt # Honor system LDFLAGS
  sed -e 's/indx > 0/*indx > 0/' -i objcryst/newmat/newmat6.cpp
}

build() {
  cd objcryst/Fox
  make Fox-nogui shared=0 -j1

  cd "$srcdir"
  cmake -B build -S genXrdPattern-$pkgver-linux \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DObjCryst_ROOT="$srcdir"/objcryst
  cmake --build build
}

package() {
  cd build
  install -Dm755 genXrdPattern -t "$pkgdir"/usr/bin
}
